package monitor;


import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import lagern.Lager;
import lagern.Monitor;
import lagern.MonitorHelper;
import lagern.MonitorPOA;

	class ShutdownActions implements Runnable {
		MonitorImpl mC;
		ShutdownActions(MonitorImpl mC) {
			this.mC = mC;
		}
		
	    public void run() {
	    	mC.abmelden();
	    }
	 }
	
	class End extends Thread {
		ORB orb;
		
		End(ORB orb){
			this.orb = orb;
		}
		
		public void run(){
			orb.shutdown(true);
		}
	}
	
public class MonitorImpl extends MonitorPOA{
	Lager lager;
	Thread hook;
	ORB orb;
	End e;
		
	public void aktion(String user, String log) {
		// TODO Auto-generated method stub
		System.out.println(log + " von: " + user);
	}
	
	public void quit() {
		Runtime.getRuntime().removeShutdownHook(hook);
		System.out.println("Monitor wird vom Server beendet");
		e = new End(orb);
		e.start();
	}
	
	public void abmelden(){
		try {
			POA rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootPoa.the_POAManager().activate();
			org.omg.CORBA.Object ref = rootPoa.servant_to_reference(this);
			Monitor href = MonitorHelper.narrow(ref);
			lager.entferneMonitor(href);	
		}catch (Exception e) {
			System.err.println("ERROR: " + e);
			e.printStackTrace(System.out);	
		}
		orb.shutdown(true);
	}
		
}
